package com.zonski.jbots.desktop.editer;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 27, 2003
 * Time: 9:16:03 AM
 * To change this template use Options | File Templates.
 * <p>
 * Managers the editers on the canvas
 */
public class ToolManager
{
    private ArrayList tools;
    private JBotsEditerContext context;

    public ToolManager(JBotsEditerContext context)
    {
        this.tools = new ArrayList();
        this.context = context;
    }

    public void addTool(Tool tool)
    {
        tool.setContext(this.context);
        this.tools.add(tool);

        ToolListener toolListener = new ToolListener()
        {
            public void toolActivated(ToolEvent event)
            {
                // deactivate current tool
                deactivateAllBut(event.getTool());
            }

            public void toolDeactivated(ToolEvent event)
            {
                // do nothing
            }
        };
        tool.addToolListener(toolListener);
    }

    private final void deactivateAllBut(Tool tool)
    {
        for(int i=0; i<tools.size(); i++)
        {
            Tool found = (Tool)tools.get(i);
            if(found != tool)
            {
                found.deactivate();
            }
        }
    }
}
